* Data preparation for second stage
*
clear all
set mem 2g
set more off
use MMO_C_regfile_nonmfg
table year mne
cap drop nc8 hs6 export qty
*
qui bysort firmid year: keep if _n == 1
merge 1:1 firmid year using tradeshocks
drop if _merge == 2
drop _merge
*IMPORT SHOCKS
merge 1:1 firmid year using impshocks
drop if _merge == 2
drop _merge
*
drop if year == 1995
*
*
*generate variables
tab year, gen(yd)
gen lemp = ln(employment)
gen lva=ln(valueadded_def)
gen linv = ln(capital/employment)
gen ltotexport = ln(totexport)
gen lsales = ln(sales)
gen lmat = ln(material)
gen lmatraw = ln(rawmaterial)
label var ltotexport "ln exports"
label var lmat "ln materials"
label var lmatraw "ln raw materials"
*
tsset firmid year
*
*FD variables construction
global nbnc8 "nbnc8"
gen lnbnc8 = ln(nbnc8)
global lnbnc8 "lnbnc8"

gen lnbnc8iso2 = ln(nbnc8iso2)
label var lnbnc8 "log count nc8 products"
label var lnbnc8iso2 "log av. count of nc8/destination"
*
tsset firmid year
*
foreach x of varlist llaborpty llaborpty_def  lratioww*   $nbnc8 $lnbnc8  lnbnc8iso2 linv lsales lmat lmatraw ltotexport {
gen `x'_d1 = D1.`x'
}

*variables for scale effects
gen byte exportup = totexport > L1.totexport
gen byte salesup = sales > L1.sales
gen byte empup = employment > L1.employment
sum exportup
*
********************************
*trade shocks with interactions*
********************************
**** Don't want to use interacted measure if exportintens is zero
foreach x in cur lag 1st {
replace exportintens`x' = .z if exportintens`x'==0
}
*
global exportintensFE "exportintens1st"
global exportintensFD "exportintenslag"
*
gen lgdpshock3 = (shrexptimesGDP1)
gen lgdpshock6 = (shrexptimesGDP2)
gen lgdpcapshock3 = (shrexptimesGDPc1)
gen lgdpcapshock6 = (shrexptimesGDPc2)
gen ltradeshock3 = (shrexptimesWLD1)
gen ltradeshock3d = (shrexptimesWLDdm1)
gen ltradeshock6 = (shrexptimesWLD2)
gen ltradeshock6d = (shrexptimesWLDdm2)
gen ltradeshock3i = (shrexptimesWLDi1)
gen ltradeshock6i = (shrexptimesWLDi2)
gen ltradeshock3ii = (shrexptimesINTFLOW1)
gen ltradeshock6ii = shrexptimesINTFLOW2
*
*FD
gen lgdpshock7 = shrexptimesGDP3
gen lgdpcapshock7 = shrexptimesGDPc3
gen ltradeshock7 = shrexptimesWLD3
gen ltradeshock7i= shrexptimesWLDi3
gen shock7intens = ltradeshock7 * $exportintensFD
*
gen gdp3intens = lgdpshock3  * ($exportintensFE )
gen gdp6intens = lgdpshock6 * $exportintensFD
gen shock3intens = ltradeshock3 * ($exportintensFE )
gen shock6intens = ltradeshock6 * $exportintensFD
gen shock3intensd = ltradeshock3d * ($exportintensFE )
gen shock6intensd = ltradeshock6d * $exportintensFD
gen shock3intensi = ltradeshock3i *($exportintensFE )
gen shock6intensi = ltradeshock6i * $exportintensFD
gen shock3intensii = ltradeshock3ii * ($exportintensFE )
gen shock6intensii = ltradeshock6ii * $exportintensFD
*
*IMPORTS
global importintensFE "importintens1st"
global importintensFD "importintenslag"
*
gen lgdpshock3_imp = (shrimptimesGDP1)
gen lgdpshock6_imp = (shrimptimesGDP2)
gen ltradeshock3_imp = (shrimptimesWLD1)
gen ltradeshock6_imp = (shrimptimesWLD2)
gen ltradeshock3i_imp = (shrimptimesWLDi1)
gen ltradeshock6i_imp = (shrimptimesWLDi2)
gen gdp3intens_imp = lgdpshock3_imp  * ($importintensFE )
gen gdp6intens_imp = lgdpshock6_imp * $importintensFD
gen shock3intens_imp = ltradeshock3_imp * ($importintensFE )
gen shock6intens_imp = ltradeshock6_imp * $importintensFD
gen shock3intensi_imp = ltradeshock3i_imp *($importintensFE )
gen shock6intensi_imp = ltradeshock6i_imp * $importintensFD
*
**********************************************
* Skewness based on export data (not predicted)
**********************************************
gen theilwintens = theilw * exportintenscur
gen theilw0intens = theilw0 * exportintenscur
gen entropwintens = entropw * exportintenscur
*
foreach x of varlist theilw theilw0 entropw theilwintens theilw0intens entropwintens{
gen `x'_d1 = D1.`x'
}
*
gen theilwintens_dconst = theilw_dconst * $exportintensFD
*
* leads and lags of shocks
foreach x of varlist shock3intens shock6intens ltradeshock3 ltradeshock3i ltradeshock6 ltradeshock6i lgdpshock3 lgdpshock6 {
qui gen `x'_l2 = L2.`x'
qui gen `x'_l1 = L1.`x'
qui gen `x'_f1 = F1.`x'
qui gen `x'_f2 = F2.`x'
}
*
label var lgdpshock3 "log GDP Shock"

label var lgdpshock6 "$ \tilde{\Delta}$ GDP Shock"
label var lgdpshock6_l1 "$\tilde{\Delta}$ GDP Shock ($ t-1$)"
label var lgdpshock6_f1 "$\tilde{\Delta}$ GDP Shock ($ t+1$)"
label var lgdpshock6_l2 "$\tilde{\Delta}$ GDP Shock ($ t-2$)"
label var lgdpshock6_f2 "$\tilde{\Delta}$ GDP Shock ($ t+2$)"

label var lgdpcapshock3 "log GDP per cap shock"
label var lgdpcapshock6 "$\tilde{\Delta}$ GDP per cap Shock"


label var ltradeshock3 "log trade shock"
label var ltradeshock3_l1 "log trade shock ($ t-1$)"
label var ltradeshock3_f1 "log trade shock ($ t+1$)"
label var ltradeshock3_l2 "log trade shock ($ t-2$)"
label var ltradeshock3_f2 "log trade shock ($ t+2$)"

label var ltradeshock6 "$ \tilde{\Delta}$ trade shock"
label var ltradeshock6_l1 "$\tilde{\Delta}$ trade shock ($ t-1$)"
label var ltradeshock6_f1 "$\tilde{\Delta}$ trade shock ($ t+1$)"
label var ltradeshock6_l2 "$\tilde{\Delta}$ trade shock ($ t-2$)"
label var ltradeshock6_f2 "$\tilde{\Delta}$ trade shock ($ t+2$)"

label var ltradeshock3i "log trade shock - ISIC"
label var ltradeshock3i_l1 "log trade shock - ISIC ($ t-1$)"
label var ltradeshock3i_f1 "log trade shock - ISIC ($ t+1$)"
label var ltradeshock3i_l2 "log trade shock - ISIC ($ t-2$)"
label var ltradeshock3i_f2 "log trade shock - ISIC ($ t+2$)"

label var ltradeshock6i "$ \tilde{\Delta}$ trade shock - ISIC"
label var ltradeshock6i_l1 "$\tilde{\Delta}$ trade shock - ISIC ($ t-1$)"
label var ltradeshock6i_f1 "$\tilde{\Delta}$ trade shock - ISIC ($ t+1$)"
label var ltradeshock6i_l2"$\tilde{\Delta}$ trade shock - ISIC ($ t-2$)"
label var ltradeshock6i_f2 "$\tilde{\Delta}$ trade shock - ISIC ($ t+2$)"

*
label var gdp3intens  "log GDP shock\_intens"
label var gdp6intens  "$ \Delta$ (GDP Shock $\times$ export intens.)"

label var shock3intens  "log trade shock\_intens"
label var shock3intens_l1  "log trade shock\_intens ($ t-1$)"
label var shock3intens_f1  "log trade shock\_intens ($ t+1$)"
label var shock3intens_l2  "log trade shock\_intens ($ t-2$)"
label var shock3intens_f2  "log trade shock\_intens ($ t+2$)"

label var shock3intens_imp "log import trade shock\_intens"

label var shock6intens  "$\tilde{\Delta}$ trade shock\_intens"
label var shock6intens_l1  "$\tilde{\Delta}$ trade shock\_intens ($ t-1$)"
label var shock6intens_f1  "$\tilde{\Delta}$ trade shock\_intens ($ t+1$)"
label var shock6intens_l2  "$\tilde{\Delta}$ trade shock\_intens ($ t-2$)"
label var shock6intens_f2  "$\tilde{\Delta}$ trade shock\_intens ($ t+2$)"


label var shock6intens_imp  "$\tilde{\Delta}$ import trade shock\_intens"
label var shock7intens  "$ \Delta$ (trade shock $\times$ export intens.)"
*
label var shock3intensi  "log trade shock\_intens - ISIC"
label var shock6intensi  "$\Delta$ (Trade Shock $\times$ Export Intens.) - ISIC"
label var shock3intensii  "Int. Flow Shock $\times$ Export Intens.) - ISIC"
label var shock6intensii  "$\Delta$ (Int. Flow Shock $\times$ Export Intens.) - ISIC"
*
label var theilw "Skewness of World Exports (Theil)"
label var theilw_d1 " $ \Delta$ skewness"
label var theilwintens " Skewness $\times$ Export Intens."
label var theilwintens_d1 " $ \Delta$ skewness $\times$ Export Intens."
*
label var entropw "Skewness of World Exports (Entrop)"
label var entropw_d1 " $ \Delta$ Skewness"
label var entropwintens " Skewness $\times$ Export Intens."
label var entropwintens_d1 " $ \Delta$ Skewness $\times$ Export Intens."
*
label var linv "log capital stock per worker"
label var linv_d1 " $ \Delta$ log capital stock per worker"
*
label var lmatraw "log raw materials"
label var lmatraw_d1 "$ \Delta$ log raw materials"
*
gen exportintens_d1 = exportintenscur-exportintenslag
gen lexportintens_d1 = log(exportintenscur)-log(exportintenslag)
*
save temp1_nonmfg,replace
*
* export quantile of first year in sample
sort firmid year
qui bysort firmid: keep if _n == 1
sum exportintenscur, d
xtile expintens_quant = exportintenscur, n(4)
keep firmid expintens_quant
save temp000,replace
use temp1_nonmfg,clear
merge m:1 firmid using temp000
drop _merge
label var expintens_quant "export intensity quantile based on first year of export"
erase temp000.dta
*
save temp1_nonmfg,replace
